<?php
include('conect.php');

//connection String
$con = mysql_connect($hostname, $username, $password) or die('Could not connect: ' . mysql_error());
//select database
mysql_select_db($database, $con);

mysql_query("SET NAMES utf8");
$acction = $_GET['acction'];
//$subpro = $_GET['subpro'];
//$cild = $_GET['cild'];
//$pro = $_GET['pro'];
$arr = array();

//$sql = "SELECT * FROM date";
	//$rs = mysql_query($sql);
	//$grid = array();
	//while($obj = mysql_fetch_object($rs)) {
	//	$grid[] = $obj;
	//}
	//$arr = array(
	//	'rows'  => $grid
	//);

if ($acction === 'dateout') {
	//$sql = "SELECT d.id, d.denum, c.task AS cild, d.cild AS idcild, d.data, d.suma, s.task AS subpro, d.subpro AS idsubpro, d.tip FROM date AS d, cild AS c, subpro AS s WHERE d.cild=c.id AND d.subpro=s.id AND d.user='".$_GET['UserID']."' ORDER by d.data DESC";
	//$rs = mysql_query($sql);
	$grid = array();
	$gri = array();
	//while($obj = mysql_fetch_object($rs)) {
	//	$grid[] = $obj;
	//}
	$data = date("W");
	$tr  = date("d");
	 		$gri = array(
		 		'name' => $tr,
		 		'sun' => $data+1,
		 		'mon' => $data+2,
		 		'tue' => $data+3,
		 		'wed' => $data+4,
		 		'thu' => $data+5,
		 		'fri' => $data+6,
		 		'sat' => $data+7,			
	 		);
	 		$grii = array(
	 		'name' => $data+1,
	 		'sun' => $data+8,
	 		'mon' => $data+9,
	 		'tue' => $data+10,
	 		'wed' => $data+11,
	 		'thu' => $data+12,
	 		'fri' => $data+13,
	 		'sat' => $data+14,	
	 		);
	 		$griii = array(
	 		'name' => $data+2,
	 		'sun' => $data+15,
	 		'mon' => $data+16,
	 		'tue' => $data+17,
	 		'wed' => $data+18,
	 		'thu' => $data+19,
	 		'fri' => $data+20,
	 		'sat' => $data+21,	
	 		);
	 		$griiii = array(
	 		'name' => $data+3,
	 		'sun' => $data+22,
	 		'mon' => $data+23,
	 		'tue' => $data+24,
	 		'wed' => $data+25,
	 		'thu' => $data+26,
	 		'fri' => $data+27,
	 		'sat' => $data+28,	
	 		);
	 		//}
	 	//$grid = $gri;
	 	//$ar = array( $gri);
	 	
	// }
	$arr = array(
		$gri, $grii, $griii, $griiii
	);
}

if ($acction === 'pro') {
	$query = "SELECT d.id, d.denum, c.task AS cild, d.cild AS idcild, d.data, d.suma, s.task AS subpro, d.subpro AS idsubpro, d.tip FROM date AS d, cild AS c, subpro AS s WHERE d.cild=c.id AND d.subpro=s.id AND d.pro='".$_GET['pro']."' AND d.user='".$_GET['UserID']."' ORDER by d.data DESC";
	$rs = mysql_query($query);
	$grid = array();
	while($obj = mysql_fetch_object($rs)) {
		$grid[] = $obj;
	}
	$arr = array(
		'rows'  => $grid
	);
}

if ($acction === 'categ') {
	$sql = "SELECT d.id, d.denum, c.task AS cild, d.cild AS idcild, d.data, d.suma, s.task AS subpro, d.subpro AS idsubpro, d.tip FROM date AS d, cild AS c, subpro AS s WHERE d.cild=c.id AND d.subpro=s.id AND c.subpro='".$_GET['subpro']."' AND d.user='".$_GET['UserID']."' ORDER by d.data DESC";
	$rs = mysql_query($sql);
	$grid = array();
	while($obj = mysql_fetch_object($rs)) {
		$grid[] = $obj;
	}
	$arr = array(
		'rows'  => $grid
	);
}

if ($acction === 'cild') {
	$query = "SELECT d.id, d.denum, c.task AS cild, d.cild AS idcild, d.data, d.suma, s.task AS subpro, d.subpro AS idsubpro, d.tip FROM date AS d, cild AS c, subpro AS s WHERE d.cild=c.id AND d.subpro=s.id AND d.cild='".$_GET['cild']."' AND d.user='".$_GET['UserID']."' ORDER by d.data DESC";
	$rs = mysql_query($query);
	$grid = array();
	while($obj = mysql_fetch_object($rs)) {
		$grid[] = $obj;
	}
	$arr = array(
		'rows'  => $grid
	);
}

mysql_close($con);
echo json_encode($arr);

?>